package com.gyg.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.gyg.client.CloudPictureClient;
import com.gyg.domain.CloudUser;
import com.gyg.enums.SessionEnum;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.utils.OSSUtil;
import com.gyg.vo.param.DownloadParam;
import com.gyg.vo.param.FileParam;
import com.gyg.vo.param.UpdateFileParam;
import com.gyg.vo.result.FileDownloadResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@Api(value = "图片模块", tags = "图片模块")
@RequestMapping("/cloud-picture")
public class CloudPictureController {

    @Resource
    private OSSUtil ossUtil;

    @Resource
    private CloudPictureClient cloudPictureClient;

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片")
    public RespEntity<String> upload(MultipartFile file_data, HttpSession session){
        log.info("fileName={}", file_data.getOriginalFilename());
        String fileName = ossUtil.getFileName("cloud/picture/", file_data.getOriginalFilename());
        String url = ossUtil.getUrl(fileName);
        try {
            byte[] bytes = file_data.getBytes();
            ossUtil.upload(fileName, bytes);

            //数据组装
            CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
            FileParam fileParam = FileParam.builder()
                    .userId(cloudUser.getId())
                    .filename(file_data.getOriginalFilename())
                    .url(url)
                    .size(file_data.getSize())
                    .type(FilenameUtils.getExtension(file_data.getOriginalFilename()))
                    .build();
            RespEntity respEntity = cloudPictureClient.commitPicture(fileParam);
            return respEntity;
        } catch (Exception e) {
            log.error("图片上传失败", e.getMessage());
            throw new RuntimeException();
        }
    }

    @PostMapping("/download")
    @ApiOperation(value = "图片下载")
    public RespEntity download(DownloadParam downloadParam, HttpServletResponse response) {
        RespEntity<FileDownloadResult> respEntity = cloudPictureClient.download(downloadParam);
        FileDownloadResult fileDownloadResult = (FileDownloadResult) respEntity.getData();
        ossUtil.download(response, fileDownloadResult.getObjectName(), fileDownloadResult.getFilename());
        return new RespEntity(RespCode.SUCCESS);
    }

    @ApiOperation(value = "图片列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "content", value = "搜索内容", required = false)
    })
    public RespEntity cloudPictureList(@RequestParam(value = "pageNum", required = true) Integer pageNum,
                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                       @RequestParam(value = "content", required = false) String content,
                                       HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        RespEntity respEntity = cloudPictureClient.cloudPictureList(cloudUser.getId(), pageNum, pageSize, content);
        return respEntity;
    }

    @ApiOperation(value = "编辑图片")
    @PutMapping("/update")
        public RespEntity editPicture(UpdateFileParam updateFileParam) {
        RespEntity respEntity = cloudPictureClient.updatePicture(updateFileParam);
        return respEntity;
    }

    @ApiOperation(value = "删除图片")
    @DeleteMapping("/delete")
    @ApiImplicitParam(paramType = "delete", dataType = "Integer", name = "pictureId", value = "图片id", required = true)
    public RespEntity deletePicture(@RequestParam(value = "pictureId", required = true) Integer pictureId) {
        RespEntity respEntity = cloudPictureClient.deletePicture(pictureId);
        return respEntity;
    }

    @ApiOperation(value = "图片云盘信息")
    @GetMapping("/info")
    public RespEntity pictureCloudInfo(HttpSession session) {
        CloudUser cloudUser = (CloudUser) session.getAttribute(SessionEnum.USER.getCode());
        RespEntity respEntity = cloudPictureClient.pictureCloudInfo(cloudUser.getId());
        return respEntity;
    }
}
